package com.tang.controller;

//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.pojo.Books;
import com.tang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
//import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author Tom
 * @ClassName BookController
 * @Date 2022/1/1 13:10
 */
@Controller
public class BookController {
    @Autowired
    private BookService bookService;
//    @Autowired
//    private RedisTemplate<String, Object> redisTemplate;
//    //查询所有书
    @RequestMapping("/index")
    public String queryBooks(Model model,@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage) {
        PageHelper.startPage(currentPage,5);
        List<Books> books = bookService.queryAllBook();
        PageInfo page = new PageInfo(books);
        model.addAttribute("list", books);
        model.addAttribute("page", page);
        return "index";
    }
    //跳转书籍添加页
    @RequestMapping("/addBook")
    public String addBook(Model model) {

        return "addBook";
    }
//    @RequestMapping("/toAddBook")
//    public String addBook() {
//        return "addBook";
//    }
//
    //添加书籍后,重定向到首页
    @RequestMapping("/addSuccess")
    public String addSuccess(Books books, HttpServletRequest request) {
        System.out.println("成功添加" + books);
        bookService.addBook(books);
//        redisTemplate.opsForValue().set("book",books);
        return "redirect:index";
    }
}
    